import PySimpleGUI as sg
import time
from openpyxl import load_workbook

# 我是好人
class Random_Choose():
    def __init__(self):
        sg.theme('BluePurple')
        layout = [[sg.Text('选择Excel', size=(8, 1)), sg.Input(key='Input'), sg.FileBrowse(key='file_path'),
                   sg.InputCombo([1, 2, 3], default_value='选择列', size=(6, 1), key='combo'),
                   sg.Button('导入', key='import_data')],
                  [sg.Text('')],
                  [sg.Text('', size=(8, 2), font=('Helvetica', 25),
                           justification='center', key='text'), sg.Text('', border_width=10),
                   sg.Button('开始', key='-RUN-PAUSE-', button_color=('white', '#001480'), size=(10, 2)),
                   sg.Exit(button_color=('white', 'firebrick4'), key='Exit', button_text='退出', size=(10, 2))]]
        # [sg.Button('开始', key='-RUN-PAUSE-', button_color=('white','#001480')),
        #  sg.Exit(button_color=('white', 'firebrick4'), key='Exit', button_text='退出')]]
        self.window = sg.Window('随机选人器', layout,
                                auto_size_buttons=False,
                                grab_anywhere=False)
        self.name_num = 0

        self.current_time, self.paused_time, self.paused = 0, 0, False
        self.start_time = self.time_as_int()

        self.ls = []
        self.row_num = 0

    def Excel_handle(self):
        file_path = self.values['file_path']
        wb = load_workbook(file_path)  # 读取工作表
        ws = wb.worksheets[0]

        # print(len(ws["c"]))
        # if row == 1:

        for i in range(2, len(ws[self.row_num])):
            value = ws["{0}{1}".format(self.row_num, i)].value
            self.ls.append(value)
        # print(list1)
        # num = random.randint(0, len(list1))
        # print(list1[num])

    def time_as_int(self):
        return int(round(time.time() * 100))

    def row_judge(self):
        if self.values['combo'] == 1:
            self.row_num = 'a'
        elif self.values['combo'] == 2:
            self.row_num = 'b'
        else:
            self.row_num = 'c'

    def circle_event(self):
        while True:
            # --------- Read and update window --------
            if self.paused:
                event, self.values = self.window.read(timeout=10)
                self.current_time = self.time_as_int() - self.start_time
            else:
                event, self.values = self.window.read()
            # --------- Do Button Operations --------
            if event in (sg.WIN_CLOSED, 'Exit'):  # ALWAYS give a way out of program
                break
            self.row_judge()
            if event == 'import_data':
                self.Excel_handle()
            elif event == '-RUN-PAUSE-':
                self.paused = not self.paused
                if self.paused:
                    self.paused_time = self.time_as_int()
                else:
                    self.start_time = self.start_time + self.time_as_int() - self.paused_time
                    sg.popup_quick_message("恭喜这个B:{}".format(self.ls[self.name_num]), title="提醒")
                # Change button's text
                self.window['-RUN-PAUSE-'].update(
                    '停止' if self.paused else '开始')  # 如果pause==1，则button文本更新为run，否则更新为pause

            self.window['text'].update(self.ls[self.name_num])
            if self.current_time % 3 == 1:
                self.name_num += 1
            if self.name_num == len(self.ls):
                self.name_num = 0

        self.window.close()


if __name__ == '__main__':
    choose = Random_Choose()
    choose.circle_event()
